package com.zxy.study.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners;
import com.zxy.study.R;
import com.zxy.study.utils.DimensUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : Shonn.Zhang
 * @email : shonn.zhang@mart.blue
 * @time : 2020/9/23 10:12
 * @desc :
 */
public class ScrollToPositionActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView mRecyclerView;
    private List<String> mImageList;
    private EditText mEditText;

    private LinearLayoutManager mLayoutManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll_to_pos);
        initView();
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.recyclerview);
        mLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mImageList = getImageList();
        ImageAdapter mAdapter = new ImageAdapter();
        mRecyclerView.setAdapter(mAdapter);

        mEditText = findViewById(R.id.edit_text);
        findViewById(R.id.btn_to_pos).setOnClickListener(this);
        findViewById(R.id.btn_smooth_to_pos).setOnClickListener(this);
    }

    private List<String> getImageList() {
        List<String> imageList = new ArrayList<>();
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600837970340&di=f5c7da257f788b78b9f7ac8bc937abb7&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F57%2F28%2F01300000921826141405283668131.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600837970342&di=881745d35121f65501bfe10107354c3a&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F56%2F12%2F01300000164151121576126282411.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600837970342&di=a19793132c02e810c982498419797a6f&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F70%2F91%2F01300000261284122542917592865.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600837970341&di=68c1d882b5b27e7bd6860a05a41e0b97&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F22%2F59%2F19300001325156131228593878903.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600837970339&di=f3d1bb7803ab27c52d71ad27c3fd13f1&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F27%2F81%2F01200000194677136358818023076.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600837970339&di=064cf570cd98e5b59121abea80c1a9b6&imgtype=0&src=http%3A%2F%2Fattachments.gfan.com%2Fforum%2Fattachments2%2Fday_110320%2F11032021067b907d3ed754dd93.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600837970337&di=953e265621c5ce4114b6bc1a7142bda2&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F25%2F99%2F19300000421423134190997943822.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600837970337&di=7f559ef31904901be16f57a0d2f745ee&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F14%2F68%2F19300001338674131496682910142.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600839807307&di=59cc770aa0c9a58fd89ce61f92bf90e3&imgtype=0&src=http%3A%2F%2F3732784.s21i-3.faidns.com%2F2%2FABUIABACGAAgxaP3qQUogPOy4QIwywg44AU.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600839820862&di=ba83a7d1bdf1c4970c7847a79fad9e6d&imgtype=0&src=http%3A%2F%2Fdfscdn.dfcfw.com%2Fdownload%2FA203BG6B5OP6MF78FJGD");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600839820861&di=7f9af3430f519109ad2de63f150deaa0&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F20%2F56%2F19300001056606131348564606754.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600839820861&di=6d6e1da28ede36d2ffbdfa11e9164ffd&imgtype=0&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F241f95cad1c8a786077495386509c93d70cf50ae.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600839820860&di=2dd62b33df3ff2d38cafe0f9b16a5b0b&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F48%2F85%2F01300000190639122695850379005.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600839820860&di=7a1c0b56d225d6a5d4b970f4ba93b995&imgtype=0&src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201507%2F23%2F20150723232238_3Ydci.jpeg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600839820859&di=6847aa01a774fe80377aef55b4dadc22&imgtype=0&src=http%3A%2F%2Fdl.ppt123.net%2Fpptbj%2F201203%2F2012032616594705.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600839820859&di=e86c02bcfe446668331e240a72aafbaa&imgtype=0&src=http%3A%2F%2Fpic30.nipic.com%2F20130614%2F9779386_084804449000_2.jpg");
        imageList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600839820858&di=29173b98802af665d04e0821b1d1ac4f&imgtype=0&src=http%3A%2F%2Ff.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F86d6277f9e2f07085d8dbe9feb24b899a801f2a1.jpg");

        return imageList;
    }

    @Override
    public void onClick(View v) {
        String input = mEditText.getText().toString();
        int pos = 0;
        if (!TextUtils.isEmpty(input)) {
            pos = Integer.parseInt(input);
        }
        switch (v.getId()) {
            case R.id.btn_to_pos:
                mRecyclerView.scrollToPosition(pos);
                break;
            case R.id.btn_smooth_to_pos:
                smoothToPosToTop(pos);
                break;
        }
    }

    private void smoothToPosToTop(int pos) {
        RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(this) {

            @Override
            protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                return super.calculateSpeedPerPixel(displayMetrics) * 2;
            }

            @Override
            protected int getVerticalSnapPreference() {
                return SNAP_TO_START;
            }
        };
        smoothScroller.setTargetPosition(pos);
        mLayoutManager.startSmoothScroll(smoothScroller);
    }


    private class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ImageView imageView = new ImageView(ScrollToPositionActivity.this);
            ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DimensUtil.dp2px(ScrollToPositionActivity.this, 150));
            params.bottomMargin = 15;
            params.leftMargin = 15;
            params.rightMargin = 15;
            imageView.setLayoutParams(params);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            return new RecyclerView.ViewHolder(imageView) {
            };
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ImageView imageView = (ImageView) holder.itemView;
            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
            layoutParams.height = DimensUtil.dp2px(ScrollToPositionActivity.this, position == 3 ? 600 : 150);

//            Glide.with(ScrollToPositionActivity.this).load(mImageList.get(position)).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView);
            Glide.with(ScrollToPositionActivity.this).load(mImageList.get(position)).transform(new GranularRoundedCorners(12f,12f,12f,12f)).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView);
        }

        @Override
        public int getItemCount() {
            return mImageList == null ? 0 : mImageList.size();
        }
    }

}
